negotiator.distributedtournament
Class DBController

java.lang.Object
  extended by negotiator.distributedtournament.DBController

public class DBController
extends java.lang.Object

Creates a DBControler. The DBController is used to manage the communication with the database in case a distributed tournament is ran. In this implementation, it is assumed that we are communicating with a busy webserver. Therefore, each time a group of operations has been performed, the connection is closed.

Version:
17-12-11
Author:
Mark Hendrikx

Method Summary
 java.lang.Object clone()
           
static byte[] compressBytes(java.lang.String data)
          Compresses the data to be stored in the database.
static boolean connect(java.lang.String url, java.lang.String username, java.lang.String password)
          Connect to the database.
 void createJob(java.lang.String sessionName, negotiator.tournament.Tournament t)
          Creates a job by storing the tournament in the database, and splitting all sessions into smaller groups.
 boolean existsSessionName(java.lang.String sessionName)
          Returns if a sessionname exists.
static java.lang.String extractBytes(byte[] input)
          Decompress a compressed string.
static java.lang.String getDistributedTutorial()
           
static DBController getInstance()
           
 Job getJob(int jobID, java.util.ArrayList<negotiator.protocol.Protocol> sessions)
          Gets a group of sessions to run.
 int getJobID(java.lang.String sessionname)
          Returns the last issued job with the given session name.
 int getMatchesPerSession(int jobID)
          Returns the amount of matches per session.
 int getRunningSessions(int jobID)
          Get the amount of jobs which are currently executed by other instances of Genius or need to be processed.
 negotiator.tournament.Tournament getTournament(int jobID)
          Given the jobID of the tournament, the tournament is requested from the database and stored as a Tournament object.
static boolean reconnect()
          Reconnect to the database.
 void reconstructLog(int jobID)
          Reconstructs the full log of outcomes by gluing all separate outcomes of the job together.
 void resetJobs(int jobID)
          Resets all sessions which were busy.
 void storeResult(int sessionID, java.lang.String outcome)
          Store the result of the group of sessions in the DB.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static DBController getInstance()
Returns:
instance of the database controller with a connection

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

connect

public static boolean connect(java.lang.String url,
                              java.lang.String username,
                              java.lang.String password)
Connect to the database.

Parameters:
url - with port (also supports properties: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html)
username - of the database account
password - of the database account
Returns:
true if connection was succesfull

reconnect

public static boolean reconnect()
Reconnect to the database.

Returns:
true if success

createJob

public void createJob(java.lang.String sessionName,
                      negotiator.tournament.Tournament t)
Creates a job by storing the tournament in the database, and splitting all sessions into smaller groups.

Parameters:
sessionName - name of the session given to the tournament
t - tournament from which the jobs are derived.

getJobID

public int getJobID(java.lang.String sessionname)
Returns the last issued job with the given session name.

Parameters:
sessionname - name of the session.
Returns:
jobID if success, else 0

getTournament

public negotiator.tournament.Tournament getTournament(int jobID)
Given the jobID of the tournament, the tournament is requested from the database and stored as a Tournament object.

Parameters:
jobID - of the tournament to be retrieved
Returns:
tournament object with the given jobID

getJob

public Job getJob(int jobID,
                  java.util.ArrayList<negotiator.protocol.Protocol> sessions)
Gets a group of sessions to run.

Parameters:
jobID - id of the main job.
sessions - array of all jobs of the tournament.
Returns:
subset of the full array of jobs which still needs to be executed.

storeResult

public void storeResult(int sessionID,
                        java.lang.String outcome)
Store the result of the group of sessions in the DB.

Parameters:
sessionID - ID of the group
outcome - outcome for the group

getRunningSessions

public int getRunningSessions(int jobID)
Get the amount of jobs which are currently executed by other instances of Genius or need to be processed.

Parameters:
jobID - ID of the high-level job
Returns:
amount of jobs which are currently running

existsSessionName

public boolean existsSessionName(java.lang.String sessionName)
Returns if a sessionname exists.

Parameters:
sessionName - name of the tournament.
Returns:
true if exists

resetJobs

public void resetJobs(int jobID)
Resets all sessions which were busy. This is to avoid a infinite waiting time when another computer has failed at processing a job (in which case it is still marked as busy).

Parameters:
jobID - ID of the high-level job

reconstructLog

public void reconstructLog(int jobID)
Reconstructs the full log of outcomes by gluing all separate outcomes of the job together. Note that the resulting log should be identical to running the full tournament on a single PC.

Parameters:
jobID - ID of the job

getMatchesPerSession

public int getMatchesPerSession(int jobID)
Returns the amount of matches per session.

Parameters:
jobID - ID of the high-level job
Returns:
amount of matches per session, or 5 if nothing was found

getDistributedTutorial

public static java.lang.String getDistributedTutorial()
Returns:
small description of the DT functionality.

compressBytes

public static byte[] compressBytes(java.lang.String data)
                            throws java.io.UnsupportedEncodingException,
                                   java.io.IOException
Compresses the data to be stored in the database. This can result in a compression rate of 1 / 60.

Parameters:
data - to be compressed.
Returns:
compressed string
Throws:
java.io.UnsupportedEncodingException - if UTF-8 encoding is not supported.
java.io.IOException - if there is a problem reading the string.

extractBytes

public static java.lang.String extractBytes(byte[] input)
                                     throws java.io.UnsupportedEncodingException,
                                            java.io.IOException,
                                            java.util.zip.DataFormatException
Decompress a compressed string.

Parameters:
input - compressed byte array which needs to be decompressed.
Returns:
decompressed string.
Throws:
java.io.UnsupportedEncodingException - if UTF-8 encoding is not supported.
java.io.IOException - if there is a problem reading the byte array.
java.util.zip.DataFormatException - should not happen.